'use strict';

define(['controllers', 'codemirror', 'codemirror-xml', 'codemirror-javascript', '../service/updateService', '../service/schemaService'], function (controllers, CodeMirror) {
    controllers.collection.register('UpdateController', ['$scope', '$routeParams', 'UpdateService', 'SchemaService', function ($scope, $routeParams, updateService, schemaService) {
        $scope.collectionId = $routeParams.id;
        $scope.DOC_TYPE_JSON = 'json';
        $scope.DOC_TYPE_XML = 'xml';
        $scope.init = function () {
            $scope.update_commit = true;
            $scope.update_overwrite = true;
            $scope.update_doc = '';
            $scope.update_docType = $scope.DOC_TYPE_JSON;

            setTimeout(function () {
                $scope.result_editor = CodeMirror.fromTextArea(document.getElementById("result_editor"), {
                    mode: "application/json",
                    styleActiveLine: true,
                    lineNumbers: true,
                    lineWrapping: true
                });
                $scope.data_editor = CodeMirror.fromTextArea(document.getElementById("data_editor"), {
                    mode: "application/json",
                    styleActiveLine: true,
                    lineNumbers: true,
                    lineWrapping: true
                });
            }, 10);

            schemaService.query($scope.collectionId, function (schema) {
                $scope.schema = schema;
                setTimeout(function() {
                    $scope.changeDocType();
                }, 10);
            });
        };

        $scope.getDefaultDoc = function () {
            var fields = $scope.schema.fields;
            var doc = '';
            if ($scope.update_docType == $scope.DOC_TYPE_JSON) {
                doc = "{\n";
                for (var i = 0; i < fields.length; i++) {
                    var field = fields[i];
                    doc += '\t"' + field.name + '":""';
                    if (i < fields.length - 1) {
                        doc += ',';
                    }
                    doc += '\n';
                }
                doc += "}";
            } else if ($scope.update_docType == $scope.DOC_TYPE_XML) {
                doc = "<doc>\n";
                for (var i = 0; i < fields.length; i++) {
                    var field = fields[i];
                    doc += '\t<field name="' + field.name + '"></field>\n';
                }
                doc += "</doc>";
            }
            return doc;
        };

        $scope.changeDocType = function () {
            $scope.update_doc = $scope.getDefaultDoc();
            if ($scope.update_docType == $scope.DOC_TYPE_JSON) {
                $scope.data_editor.setOption('mode', 'application/json');
            } else {
                $scope.data_editor.setOption('mode', 'application/xml');
            }
            $scope.data_editor.setValue($scope.update_doc);
        };

        $scope.update = function () {
            $scope.update_doc = $scope.data_editor.getValue();
            updateService.update($scope.collectionId, {
                overwrite: $scope.update_overwrite,
                commit: $scope.update_commit,
                docType: $scope.update_docType,
                doc: $scope.update_doc
            }, function (data) {
                $scope.result_editor.setValue(data);
            });
        };

        $scope.init();
    }]);
});